<?php


namespace app\index\controller;


use think\admin\Controller;
use think\facade\Db;
use think\facade\Validate;

class Article extends Controller
{
    public function index(){

    }

    public function lists(){
        $data = input();
        $rule = [
            'name'  => 'require',
            'type'  => 'require'
        ];
        $check = Validate::check($data,$rule);
        if (!$check){
            $this->error($check->getError());
        }
        $list = Db::name('shop_goods')->where('gtype='.$data['type'])->whereLike('name',$data['name'])->page(sysconf('page_limit'));
        if (!empty($list)){
            $this->success('获取成功',$list);
        }else{
            $this->error('获取失败');
        }
    }

    public function like(){
        $data = input('post.');
        $article = Db::name('shop_goods')->where('id='.$data['a_id'])->find();
        if (empty($article) || $article['deleted'] == 1){
            $this->error('文章不存在');
        }

        if ($article['status'] == 0){
            $this->error('文章已被禁用');
        }

        $result = Db::name('shop_goods')->where('id='. $data['a_id'])->setInc('num_collect',1);
        if ($result == 1){
            $this->success('点赞成功');
        }else{
            $this->error('点赞失败');
        }
    }

    public function details(){
        $data = input('post.');
        $article = Db::name('shop_goods')->where('id='.$data['a_id'])->field('id')->find();
        if (empty($article) || $article['deleted'] == 1){
            $this->error('文章不存在');
        }

        if ($article['status'] == 0){
            $this->error('文章已被禁用');
        }
    }


    /**
     * @throws \think\db\exception\DbException
     * 用户点赞
     */
    public function articleLike(){
        $data = input('post.');
        if (!empty($data)){
            $a_id = $data['a_id'];
            $info = ['a_id' => $a_id,'user_id' => MID,'type' => 1];
            $like = Db::name('data_like_step')->where($info)->value('status');
            if (!empty($like)){
                //已点赞
                if ($like == 1){
                    $res = Db::name('data_like_step')->where($info)->update(['status' => 2]);
                }
                //已取消
                else{
                    $res = Db::name('data_like_step')->where($info)->update(['status' => 1]);
                    if ($res){
                        $this->success('已取消点赞');
                    }else{
                        $this->error('取消失败');
                    }
                }
            }else{
                //未点赞过
                $info['status'] = 1;
                $res = Db::name('data_like_step')->insert($info);
            }

            if ($res){
                $this->success('已点赞');
            }else{
                $this->error('已取消');
            }
        }
    }


    /**
     * @throws \think\db\exception\DbException
     * 记录用户足迹
     */
    public function articleStack(){
        $data = input('post.');
        if (!empty($data)){
            $a_id = $data['a_id'];
            $info = ['a_id' => $a_id,'user_id' => MID,'type' => 1];
            $like = Db::name('data_like_step')->where($info)->value('status');
            if (!empty($like)){
                //已点赞
                if ($like == 1){
                    $res = Db::name('data_like_step')->where($info)->update(['status' => 2]);
                }
                //已取消
                else{
                    $res = Db::name('data_like_step')->where($info)->update(['status' => 1]);
                    if ($res){
                        $this->success('已取消点赞');
                    }else{
                        $this->error('取消失败');
                    }
                }
            }else{
                //未点赞过
                $info['status'] = 1;
                $res = Db::name('data_like_step')->insert($info);
            }

            if ($res){
                $this->success('已点赞');
            }else{
                $this->error('已取消');
            }
        }
    }

}